package org.jeese.bean;

import java.io.Serializable;
import java.util.LinkedHashMap;

/**
 * 返回Json结果集类
 * 
 * @author wubc
 * @date 2018年9月4日 下午5:55:53
 * @version V1.0
 */
public class JsonResult extends LinkedHashMap<String, Object> implements Serializable {
	private static final long serialVersionUID = 1L;
	private static final String CODE_KEY = "statusCode";
	private static final String RESULT_KEY = "result";
	private static final String MESSAGE_KEY = "message";
	private static final String DATA_KEY = "data";
	private static final String CALLBACK_TYPE_KEY = "callbackType";
	private static final String NAV_TAB__KEY = "navTabId";
	private static final String CALLBACK_TYPE_DEFAULT = "closeCurrent";

	private static final String MESSAGE_SUCCESS = "操作成功。";
	private static final String MESSAGE_FAILURE = "操作失败。";
	private static final String MESSAGE_ERROR = "操作错误。";

	private static final Integer STATUS_CODE_SUCCESS = 200;
	private static final Integer STATUS_CODE_FAILURE = 201;
	private static final Integer STATUS_CODE_ERROR = 202;

	public JsonResult() {
		put(CODE_KEY, STATUS_CODE_SUCCESS);
		put(RESULT_KEY, Boolean.TRUE);
		put(MESSAGE_KEY, MESSAGE_SUCCESS);
	}

	/**
	 * 请求成功
	 * 
	 * @return
	 */
	public JsonResult success() {
		put(CODE_KEY, STATUS_CODE_SUCCESS);
		put(RESULT_KEY, Boolean.TRUE);
		put(MESSAGE_KEY, MESSAGE_SUCCESS);
		return this;
	}

	/**
	 * 请求成功,并带消息
	 * 
	 * @param message
	 * @return
	 */
	public JsonResult success(String message) {
		put(CODE_KEY, STATUS_CODE_SUCCESS);
		put(RESULT_KEY, Boolean.TRUE);
		put(MESSAGE_KEY, message);
		return this;
	}

	/**
	 * @param obj
	 * @return
	 */
	public JsonResult successData(Object data) {
		put(CODE_KEY, STATUS_CODE_SUCCESS);
		put(RESULT_KEY, Boolean.TRUE);
		put(MESSAGE_KEY, MESSAGE_SUCCESS);
		put(DATA_KEY, data);
		return this;
	}

	/**
	 * @param message
	 * @param obj
	 * @return
	 */
	public JsonResult success(String message, Object data) {
		put(CODE_KEY, STATUS_CODE_SUCCESS);
		put(RESULT_KEY, Boolean.TRUE);
		put(MESSAGE_KEY, message);
		put(DATA_KEY, data);
		return this;
	}

	public JsonResult success(String message, boolean closed, String navTabId) {
		put(CODE_KEY, STATUS_CODE_SUCCESS);
		put(RESULT_KEY, Boolean.TRUE);
		put(MESSAGE_KEY, message);
		if (!(navTabId == null || "".equals(navTabId))) {
			put(NAV_TAB__KEY, navTabId);
		}
		if (closed) {
			put(CALLBACK_TYPE_KEY, CALLBACK_TYPE_DEFAULT);
		}
		return this;
	}

	public JsonResult success(boolean closed, String navTabId) {
		put(CODE_KEY, STATUS_CODE_SUCCESS);
		put(RESULT_KEY, Boolean.TRUE);
		put(MESSAGE_KEY, MESSAGE_SUCCESS);
		if (!(navTabId == null || "".equals(navTabId))) {
			put(NAV_TAB__KEY, navTabId);
		}
		if (closed) {
			put(CALLBACK_TYPE_KEY, CALLBACK_TYPE_DEFAULT);
		}
		return this;
	}

	public JsonResult validate(int count) {
		if (count > 0) {
			return success();
		} else {
			return error();
		}
	}

	/**
	 * @param count
	 * @return
	 */
	public JsonResult validate(int count, boolean closed, String navTabId) {
		if (count > 0) {
			return success(closed, navTabId);
		} else {
			return error();
		}
	}

	/**
	 * 失败
	 * 
	 * @param message
	 * @return
	 */
	public JsonResult failure() {
		put(CODE_KEY, STATUS_CODE_FAILURE);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, MESSAGE_FAILURE);
		return this;
	}

	/**
	 * 失败
	 * 
	 * @param message
	 * @return
	 */
	public JsonResult failure(String message) {
		put(CODE_KEY, STATUS_CODE_FAILURE);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, message);
		return this;
	}

	/**
	 * 错误
	 * 
	 * @param message
	 * @return
	 */
	public JsonResult error() {
		put(CODE_KEY, STATUS_CODE_ERROR);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, MESSAGE_ERROR);
		return this;
	}

	/**
	 * 错误
	 * 
	 * @param message
	 * @return
	 */
	public JsonResult error(String message) {
		put(CODE_KEY, STATUS_CODE_ERROR);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, message);
		return this;
	}

	/**
	 * 错误
	 * 
	 * @param message
	 * @return
	 */
	public JsonResult error(boolean closed, String navTabId) {
		put(CODE_KEY, STATUS_CODE_ERROR);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, MESSAGE_ERROR);
		if (!(navTabId == null || "".equals(navTabId))) {
			put(NAV_TAB__KEY, navTabId);
		}
		if (closed) {
			put(CALLBACK_TYPE_KEY, CALLBACK_TYPE_DEFAULT);
		}
		return this;
	}

	/**
	 * 错误
	 * 
	 * @param message
	 * @return
	 */
	public JsonResult error(String message, boolean closed, String navTabId) {
		put(CODE_KEY, STATUS_CODE_ERROR);
		put(RESULT_KEY, Boolean.FALSE);
		put(MESSAGE_KEY, message);
		if (!(navTabId == null || "".equals(navTabId))) {
			put(NAV_TAB__KEY, navTabId);
		}
		if (closed) {
			put(CALLBACK_TYPE_KEY, CALLBACK_TYPE_DEFAULT);
		}
		return this;
	}

}
